package day5;

import java.io.*;

/**
 * ClassName：TestBuffered
 *
 * @Author：小鱼
 * @Create：2025-08-22 9:19
 */
public class TestBuffered {
    //使用缓冲流
    public static void main(String[] args) throws Exception {
        //创建基础流对象
        FileInputStream fis = new FileInputStream("D:\\IDEA_project\\JP_Java\\week6\\src\\day5\\img.png ");
       // FileOutputStream fos = new FileOutputStream("D:\\IDEA_project\\JP_Java\\week6\\src\\day5\\img_copy.png ");

//        //查看效率
//        long start = System.currentTimeMillis();
//        int data;
//        while ((data = fis.read())!=-1){
//            fos.write(data);
//        }
//        long end = System.currentTimeMillis();
//        System.out.println("基础流拷贝时间："+(end-start)+"ms");
//        //关闭资源
//        fos.close();
//        fis.close();


        //使用缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
       // BufferedOutputStream bos = new BufferedOutputStream(fos);
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("D:\\IDEA_project\\JP_Java\\week6\\src\\day5\\img_copy2.png "));
        //缓冲区大小
        long start = System.currentTimeMillis();
        byte[] buff = new byte[1024];
        int data;
        while ((data = bis.read(buff))!=-1){
            bos.write(buff);
            //刷新
            bos.flush();
        }
        long end = System.currentTimeMillis();
        System.out.println("缓冲流拷贝时间："+(end-start)+"ms");

        //关闭流
        bos.close();
        bis.close();
    }
}
